﻿Public Class ctrStatus
    Inherits ctrPadrao

#Region "Declaração de variáveis"

    Private objStatus As New Persistencia.perStatus
    Private iIdStatus As Int32

#End Region

#Region "Propriedades"

    Public Property IDStatus() As Int32
        Get
            Return Me.iIdStatus
        End Get
        Set(ByVal value As Int32)
            Me.iIdStatus = value
        End Set
    End Property

    Public ReadOnly Property sqlConsulta() As String
        Get
            Return Me.objStatus.sqlConsulta
        End Get
    End Property

#End Region

#Region "Métodos públicos"

    Public Function salvar(ByVal iIdStatus As Int32, _
                           ByVal sDescricao As String, _
                           ByVal iPeriodicidade As Int32, _
                           ByVal iTolerancia As Int32, _
                           ByVal bTornaInativo As Boolean) As Boolean

        Dim bResultado As Boolean = False

        If iIdStatus > 0 Then
            Me.alterarStatus(iIdStatus, _
                             sDescricao, _
                             iPeriodicidade, _
                             iTolerancia, _
                             bTornaInativo)
            Me.iIdStatus = iIdStatus
            bResultado = True
        Else
            Me.iIdStatus = Me.inserirStatus(sDescricao, _
                                            iPeriodicidade, _
                                            iTolerancia, _
                                            bTornaInativo)

            bResultado = True
        End If

        Return bResultado

    End Function

    Public Function excluir(ByVal iIdStatus As Int32) As Boolean
        Dim bResultado As Boolean = True

        If bResultado Then
            Me.excluirStatus(iIdStatus)
        End If

        Return bResultado

    End Function

    Private Sub excluirStatus(ByVal iIdStatus As Int32)

        Try
            Me.objStatus.excluirStatus(iIdStatus)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub
    Public Function GeraProximoContato(ByVal iIDStatus As Int32, ByVal dtDataContato As Date) As Date
        Dim iPeriodicidade As Int32
        Dim dtbStatus As DataTable
        Dim dtProximoContato As Date
        Dim sMsg As String

        Try
            dtbStatus = Me.objStatus.selecionarCampoStatus("Periodicidade", iIDStatus)
            If (Not dtbStatus Is Nothing) Then

                iPeriodicidade = dtbStatus.Rows(0).Item("Periodicidade")

                Do
                    dtProximoContato = DateAdd(DateInterval.Day, iPeriodicidade, dtDataContato)
                    iPeriodicidade = iPeriodicidade + 1
                Loop While Not VerificaSeDiaUtil(dtProximoContato)

            Else

                sMsg = "Não foi possível gerar a data do próximo contato." & vbCrLf
                sMsg &= "Verifique o cadastro do status deste cliente."
                MsgBox(sMsg, MsgBoxStyle.Exclamation, "Aviso")
                dtDataContato = Date.MinValue

            End If

        Catch ex As Exception
            dtDataContato = Date.MinValue
            Throw New Exception("Exceção no método 'excluirStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)

        End Try
        Return dtProximoContato

    End Function
    Public Function selecionarStatus(ByVal iIdStatus As Int32) As DataRow
        Try
            Return Me.objStatus.selecionarStatus(iIdStatus)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
    End Function

    Public Function retornarCampoStatus(ByVal sCampo As String, _
                                        ByVal iIdStatus As Int32) As DataRow

        Try
            Dim dtDados As New DataTable

            dtDados = Me.objStatus.selecionarCampoStatus(sCampo, _
                                                         iIdStatus)

            If Not dtDados Is Nothing Then
                Return dtDados.Rows(0)
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Throw New Exception("Exceção no método 'retornarCampoStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region

#Region "Métodos privados"

    Private Sub alterarStatus(ByVal iIdStatus As Int32, _
                              ByVal sDescricao As String, _
                              ByVal iPeriodicidade As Int32, _
                              ByVal iTolerancia As Int32, _
                              ByVal bTornaInativo As Boolean)
        Try
            Me.objStatus.alterarStatus(iIdStatus, _
                                       sDescricao, _
                                       iPeriodicidade, _
                                       iTolerancia, _
                                       bTornaInativo)
        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Private Function inserirStatus(ByVal sDescricao As String, _
                                   ByVal iPeriodicidade As Int32, _
                                   ByVal iTolerancia As Int32, _
                                   ByVal bTornaInativo As Boolean) As Int32

        Dim iIdStatus As Int32

        Try
            iIdStatus = Me.objStatus.inserirStatus(sDescricao, _
                                                   iPeriodicidade, _
                                                   iTolerancia, _
                                                   bTornaInativo)

            Return iIdStatus

        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

#End Region
End Class
